package com.ideal.frame.institutions.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.ideal.frame.entity.AutoModel;
import com.ideal.frame.institutions.dto.InstitutionsRegdatabacklistDto;
import com.ideal.frame.institutions.entity.InstitutionsRegdatabacklistTb;
import com.ideal.frame.institutions.entity.QInstitutionsRegdatabacklistTb;
import com.ideal.frame.jpa.DSLRequest;
import com.ideal.frame.jpa.JpaRepositoryImpl;
import com.ideal.frame.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InstitutionsRegdatabacklistService {
    @PersistenceContext
    protected EntityManager entityManager;
    private JpaRepositoryImpl<InstitutionsRegdatabacklistTb, String> jpaRepository;

    QInstitutionsRegdatabacklistTb qInstitutionsRegdatabacklistTb = QInstitutionsRegdatabacklistTb.institutionsRegdatabacklistTb;


    @PostConstruct
    public void initRepo() {
        jpaRepository = new JpaRepositoryImpl<>(InstitutionsRegdatabacklistTb.class, entityManager);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(List<InstitutionsRegdatabacklistDto> institutionsRegdatabacklistDtos, String gnrlid) {
        if (CollUtil.isEmpty(institutionsRegdatabacklistDtos)) {
            return;
        }
        DSLRequest request = new DSLRequest();
        request.addWhere(qInstitutionsRegdatabacklistTb.gnrlid.eq(gnrlid));
        request.addWhere(qInstitutionsRegdatabacklistTb.delFlag.isFalse());
        List<InstitutionsRegdatabacklistTb> all = jpaRepository.findAll(request);
        Assert.isTrue(CollUtil.isEmpty(all), "机构id重复，请重新填写！");
        List<InstitutionsRegdatabacklistTb> collect = institutionsRegdatabacklistDtos.stream().map(v -> {
            InstitutionsRegdatabacklistTb institutionsRegdatabacklistTb = new InstitutionsRegdatabacklistTb();
            BeanUtil.copyProperties(v, institutionsRegdatabacklistTb);
            institutionsRegdatabacklistTb.setGnrlid(gnrlid);
            institutionsRegdatabacklistTb.setCreateTime(new Date());
            institutionsRegdatabacklistTb.setCreateUser(UserUtils.getCurrentUser().getId());
            institutionsRegdatabacklistTb.setDelFlag(false);
            return institutionsRegdatabacklistTb;
        }).collect(Collectors.toList());
        jpaRepository.saveAll(collect);
    }

    @Transactional(rollbackFor = Exception.class)
    public void edit(List<InstitutionsRegdatabacklistDto> institutionsRegdatabacklistDtos, String gnrlid) {
        if (CollUtil.isEmpty(institutionsRegdatabacklistDtos)) {
            deleteById(gnrlid);
        } else {
            DSLRequest request = new DSLRequest();
            request.addWhere(qInstitutionsRegdatabacklistTb.gnrlid.eq(gnrlid));
            request.addWhere(qInstitutionsRegdatabacklistTb.delFlag.isFalse());
            List<InstitutionsRegdatabacklistTb> all = jpaRepository.findAll(request);
            List<Long> deletes = all.stream().map(AutoModel::getId).filter(v -> institutionsRegdatabacklistDtos.stream()
                    .map(InstitutionsRegdatabacklistDto::getId).noneMatch(s -> s.equals(v))).collect(Collectors.toList());
            List<InstitutionsRegdatabacklistTb> collect = institutionsRegdatabacklistDtos.stream().map(v -> {
                InstitutionsRegdatabacklistTb institutionsRegdatabacklistTb = new InstitutionsRegdatabacklistTb();
                BeanUtil.copyProperties(v, institutionsRegdatabacklistTb);
                institutionsRegdatabacklistTb.setGnrlid(gnrlid);
                Long id = v.getId();
                if (id != null) {
                    institutionsRegdatabacklistTb.setModifyTime(new Date());
                    institutionsRegdatabacklistTb.setModifyUser(UserUtils.getCurrentUser().getId());
                } else {
                    institutionsRegdatabacklistTb.setCreateTime(new Date());
                    institutionsRegdatabacklistTb.setCreateUser(UserUtils.getCurrentUser().getId());
                    institutionsRegdatabacklistTb.setDelFlag(false);
                }
                return institutionsRegdatabacklistTb;
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(deletes)) {
                jpaRepository.update(qInstitutionsRegdatabacklistTb).set(qInstitutionsRegdatabacklistTb.delFlag, true)
                        .where(qInstitutionsRegdatabacklistTb.id.in(deletes)).execute();
            }
            jpaRepository.saveAll(collect);
        }
    }

    public List<InstitutionsRegdatabacklistDto> getByGnrlid(String gnrlid) {
        List<InstitutionsRegdatabacklistTb> all = jpaRepository.findAll(qInstitutionsRegdatabacklistTb.gnrlid.eq(gnrlid)
                .and(qInstitutionsRegdatabacklistTb.delFlag.isFalse()));
        if (CollUtil.isNotEmpty(all)) {
            return all.stream().map(v -> {
                InstitutionsRegdatabacklistDto i = new InstitutionsRegdatabacklistDto();
                BeanUtil.copyProperties(v, i);
                return i;
            }).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String gnrlid) {
        jpaRepository.update(qInstitutionsRegdatabacklistTb).set(qInstitutionsRegdatabacklistTb.delFlag, true)
                .where(qInstitutionsRegdatabacklistTb.gnrlid.eq(gnrlid)).execute();
    }
}
